#Binary parser template

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class ProjectIGI_mef(Model3D):

    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(ProjectIGI_mef,self).__init__("MEF")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_faces(self):
        
        self.inFile.seek(20, 1)
        numIndex = self.inFile.read_short()
        unk = self.inFile.read_long(2)
        print numIndex
        for i in xrange(numIndex / 3):
            print self.inFile.read_short(3)
    
    def parse_vertices(self):
        
        pass
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        header, filesize, unk1, unk2, OCEM = self.inFile.read_long(5)
        
        while self.inFile.tell() != filesize:
            chunk = self.inFile.read_string(4)
            chunkSize, four, unk = self.inFile.read_long(3)
            
            print chunk
            if chunk == "DNER":
                self.parse_faces()
            elif chunk == "XTRV":
                self.parse_vertices()
            else:
                self.inFile.seek(chunkSize, 1)
        
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = ProjectIGI_mef(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "ILFF", "MEF", "Project IGI MEF"

if __name__ == '__main__':
    
    file1 = ""
    obj = read_file(file1)